package com.edu.exam.entity.assessment.standard;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 等级量表实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：grading_scales
 *
 * 管理具体的等级划分标准，包含等级标识、分数范围、等级描述等。
 * 为成绩评定提供详细的等级对照表。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "grading_scales")
@Comment("等级量表表")
public class GradingScale {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 成绩评定标准ID
     */
    @NotNull(message = "成绩评定标准ID不能为空")
    @Column(name = "grading_standard_id", nullable = false)
    private Long gradingStandardId;

    /**
     * 等级标识
     */
    @NotBlank(message = "等级标识不能为空")
    @Size(max = 10, message = "等级标识长度不能超过10个字符")
    @Column(name = "grade", nullable = false, length = 10)
    private String grade;

    /**
     * 最低分
     */
    @NotNull(message = "最低分不能为空")
    @Column(name = "min_score", nullable = false, precision = 5, scale = 2)
    private BigDecimal minScore;

    /**
     * 最高分
     */
    @NotNull(message = "最高分不能为空")
    @Column(name = "max_score", nullable = false, precision = 5, scale = 2)
    private BigDecimal maxScore;

    /**
     * 等级描述
     */
    @Size(max = 100, message = "等级描述长度不能超过100个字符")
    @Column(name = "grade_description", length = 100)
    private String gradeDescription;

    /**
     * 排序序号
     */
    @Column(name = "sort_order")
    private Integer sortOrder;

    /**
     * 等级颜色代码
     */
    @Size(max = 7, message = "等级颜色代码长度不能超过7个字符")
    @Column(name = "color_code", length = 7)
    private String colorCode;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 验证分数范围
     */
    public boolean validateScoreRange() {
        if (minScore == null || maxScore == null) {
            return false;
        }
        return maxScore.compareTo(minScore) >= 0 && minScore.compareTo(BigDecimal.ZERO) >= 0;
    }

    /**
     * 判断分数是否在等级范围内
     */
    public boolean isScoreInRange(BigDecimal score) {
        if (score == null || minScore == null || maxScore == null) {
            return false;
        }
        return score.compareTo(minScore) >= 0 && score.compareTo(maxScore) <= 0;
    }

    /**
     * 获取等级显示文本
     */
    public String getDisplayText() {
        if (gradeDescription == null || gradeDescription.trim().isEmpty()) {
            return grade;
        }
        return grade + " - " + gradeDescription;
    }

    /**
     * 获取分数范围文本
     */
    public String getScoreRangeText() {
        return minScore + " - " + maxScore;
    }
}